package com.a1anwang.okble.server.advertise;

import ohos.utils.SequenceUuid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by a1anwang.com on 2018/5/30.
 */
public class OKBLEAdvertiseData {
    private List<SequenceUuid> mServiceUuids = new ArrayList<SequenceUuid>();
    private HashMap<Integer, byte[]> mManufacturerSpecificData = new HashMap<>();
    private Map<SequenceUuid, byte[]> mServiceData = new HashMap<>();
    private boolean mIncludeTxPowerLevel;
    private boolean mIncludeDeviceName;

    private OKBLEAdvertiseData(List<SequenceUuid> serviceUuids, HashMap<Integer, byte[]> manufacturerData,
                               Map<SequenceUuid, byte[]> serviceData, boolean includeTxPowerLevel, boolean includeDeviceName) {
        mServiceUuids = serviceUuids;
        mManufacturerSpecificData = manufacturerData;
        mServiceData = serviceData;
        mIncludeTxPowerLevel = includeTxPowerLevel;
        mIncludeDeviceName = includeDeviceName;
    }

    public boolean getIncludeDeviceName() {
        return mIncludeDeviceName;
    }

    public boolean getIncludeTxPowerLevel() {
        return mIncludeTxPowerLevel;
    }

    public List<SequenceUuid> getServiceUuids() {
        return mServiceUuids;
    }


    public HashMap<Integer, byte[]> getManufacturerSpecificData() {
        return mManufacturerSpecificData;
    }

    public Map<SequenceUuid, byte[]> getServiceData() {
        return mServiceData;
    }

    public static final class Builder {
        private List<SequenceUuid> mServiceUuids = new ArrayList<SequenceUuid>();
        private HashMap<Integer, byte[]> mManufacturerSpecificData = new HashMap<Integer, byte[]>();
        private Map<SequenceUuid, byte[]> mServiceData = new HashMap<>();

        private boolean mIncludeTxPowerLevel;
        private boolean mIncludeDeviceName;

        /**
         * 添加serviceUuid
         *
         * @param serviceUuid serviceUuid
         * @return Builder
         */
        public Builder addServiceUuid(SequenceUuid serviceUuid) {
            if (serviceUuid == null) {
                throw new IllegalArgumentException("serivceUuids are null");
            }
            mServiceUuids.add(serviceUuid);
            return this;
        }

        /**
         * 添加ServiceData
         *
         * @param serviceDataUuid servicedataUuid
         * @param serviceData     serviceData
         * @return Builder
         */
        public Builder addServiceData(SequenceUuid serviceDataUuid, byte[] serviceData) {
            if (serviceDataUuid == null || serviceData == null) {
                throw new IllegalArgumentException(
                        "serviceDataUuid or serviceDataUuid is null");
            }
            mServiceData.put(serviceDataUuid, serviceData);
            return this;
        }

        /**
         * 添加ManufacturerData
         *
         * @param manufacturerId            id
         * @param manufacturerSpecificData  data
         * @return Builder
         */
        public Builder addManufacturerData(int manufacturerId, byte[] manufacturerSpecificData) {
            if (manufacturerId < 0 || manufacturerId > 0xff) {

                throw new IllegalArgumentException(
                        "invalid manufacturerId - " + manufacturerId + " valid range:[0,0xFF]");

            }
            if (manufacturerSpecificData == null) {
                throw new IllegalArgumentException("manufacturerSpecificData is null");
            }
            mManufacturerSpecificData.put(manufacturerId, manufacturerSpecificData);
            return this;
        }

        /**
         * Set whether the device name should be included in advertise packet.
         *
         * @param includeDeviceName boolean类型
         * @return Builder
         */
        public Builder setIncludeDeviceName(boolean includeDeviceName) {
            mIncludeDeviceName = includeDeviceName;
            return this;
        }

        /**
         * build 方法
         *
         * @return OKBLEAdvertiseData
         */

        public OKBLEAdvertiseData build() {
            return new OKBLEAdvertiseData(mServiceUuids, mManufacturerSpecificData, mServiceData,
                    mIncludeTxPowerLevel, mIncludeDeviceName);
        }
    }
}
